<style>
    #febs-product xm-select {
        min-height: 30px;
        line-height: 30px;
    }

</style>

<div class="layui-fluid layui-anim febs-anim" id="febs-product" lay-title="添加商品">
    <div class="layui-row layui-col-space8 febs-container">
        <div class="layui-col-md12 layui-col-sm12 layui-col-xs12">
            <div class="layui-card" style="margin: 0 auto;">
                <div class="layui-card-header">添加商品</div>
                <div class="layui-card-body user-profile-card">

                    <form class="layui-form" action="" lay-filter="product-add">

                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">商品类型：</label>
                                <div class="layui-input-inline">
                                    <select name="productType" lay-filter="productType" id="productType">
                                    </select>
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">商品名称：</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="productName" id="productName" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">商品价格：</label>
                                <div class="layui-input-inline">
                                    <input type="number" name="productPrice" id="productPrice" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">商品规格：</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="productSpecification" id="productSpecification"
                                           class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">商品介绍：</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="productIntroduction" id="productIntroduction"
                                           class="layui-input">
                                </div>
                            </div>

                            <div class="layui-upload">
                                <button type="button" class="layui-btn" id="productImgBtn">商品主图</button>
                                <div class="layui-upload-list">
                                    <img class="layui-upload-img" id="productSrc" name="productSrc">
                                    <input type="text" name="productimg" id="productimg" class="layui-input layui-hide">
                                </div>
                            </div>

                            <div class="layui-upload">

                                <button class="layui-btn layui-btn-normal layui-btn-radius" lay-submit
                                        lay-filter="product-add-submit" id="submit">
                                    <i style="display: none; margin-left: 30px"
                                       class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i>
                                    开始上传
                                </button>
                            </div>
                        </div>
                    </form>

                    <table lay-filter="productTable" lay-data="{id: 'productTable'}"></table>

                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="user-option">
    <!--    <a lay-event="detail">-->
    <!--        <i class="layui-icon febs-edit-area febs-green">&#xe7a5;</i></a>-->
    <a lay-event="edit"><i
            class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
    <a lay-event="del"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>

<script data-th-inline="none" type="text/javascript">
    layui.use(['upload', 'jquery', 'febs', 'form', 'table'], function () {
        let $ = layui.jquery,
            febs = layui.febs,
            table = layui.table,
            form = layui.form,
            $view = $('#febs-product'),
            upload = layui.upload,
            tableIns,
            productTypeId
        form.render();


        initproductTable();

        febs.post(ctx + 'web/get/producttype', null, function (data) {
            $("#productType").empty();
            $("#productType").append(new Option("请选择商品类型", ""));
            $.each(data.data, function (index, item) {
                $("#productType").append(new Option(item.name, item.id));
            });
            layui.form.render("select");
        });

        form.on('select(productType)', function (data) {  //
            // console.log(data.value); //得到被选中的值
            productTypeId = new Number(data.value)
        });

        form.on('submit(product-add-submit)', function (data) {
            if (!data.field.productType) {
                febs.alert.warn('请输入商品类型');
                return false;
            }
            if (!data.field.productName) {
                febs.alert.warn('请输入商品名称');
                return false;
            }
            if (!data.field.productPrice) {
                febs.alert.warn('请输入商品价格');
                return false;
            }
            if (!data.field.productSpecification) {
                febs.alert.warn('请输入商品规格');
                return false;
            }
            if (!data.field.productIntroduction) {
                febs.alert.warn('请输入商品介绍');
                return false;
            }

            uploadInst.upload()

            return false;

        });

        var uploadInst = upload.render({
            elem: '#productImgBtn'
            , auto: false
            , url: ctx + 'web/add/product'
            , data: {
                "title": function () {
                    return $('#productName').val();
                },
                "price": function () {
                    return $('#productPrice').val();
                },
                "Introduction": function () {
                    return $('#productIntroduction').val();
                },
                "specification": function () {
                    return $('#productSpecification').val();
                },
                "productTypeId": function () {
                    return productTypeId;
                }
            }
            , done: function (res) {
                $("#submit").text("上传成功");
                layer.closeAll();
                window.location.reload();
            }
            , before: function (r) {
                let loading = $(this).find('.layui-icon');
                if (loading.is(":visible")) return;
                loading.show();
            }
            , error: function () { //传输失败的回调
                //请求异常回调
                $("#submit").text("上传失败");
            }
        });

        function initproductTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'productTable',
                url: ctx + 'web/get/productlist',
                method: 'post',
                page: false,
                totalRow: true,
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": "", //解析提示文本
                        data: res.data //解析数据列表
                    };
                },
                cols: [[
                    {field: 'productTypeTitle', title: '商品类型'},
                    {field: 'title', title: '商品名称'},
                    {field: 'price', title: '商品价格'},
                    {field: 'specification', title: '商品规格'},
                    {field: 'introduction', title: '商品简介'},
                    {
                        field: 'url', title: '主图', align: 'center',
                        templet: function (d) {
                            return '<div οnclick="photograph(this)"><img style="max-width: 100%;" src=' + d.url  + '></div>'
                        }
                    },
                    {title: '操作', toolbar: '#user-option', width: 120}
                ]]
            });
        }

        table.on('tool(productTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            console.log(data)
            if (layEvent === 'del') {
                febs.modal.confirm('删除商品', '确定删除该商品？', function () {
                    febs.post(ctx + 'web/delete/product', {id: data.id}, function () {
                        febs.alert.success('删除商品成功');
                        layer.closeAll();
                        window.location.reload();
                    });
                });
            }
            if (layEvent === 'edit') {

                febs.modal.open('修改商品', 'product/editproduct/' + data.id, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#product-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });


            }
        });
    })


</script>